package com.monitor.zk.zookeeper.util;


import bean.ZkConfig;

import java.lang.reflect.Field;

public class FieldUtils {

    /**
     * 检测数据不为空
     * @param field
     * @throws Exception
     */
    public static void checkIsNotNullFieldByType(Field field, ZkConfig zkConfig) throws Exception {
        switch (field.getType().toString()){
            case "int" :
                if(field.getInt(zkConfig) == 0){
                    throw new NullPointerException(field.getName());
                }
                break;
            case "long" :
                if(field.getLong(zkConfig) == 0){
                    throw new NullPointerException(field.getName());
                }
                break;
            case "short" :
                if(field.getShort(zkConfig) == 0){
                    throw new NullPointerException(field.getName());
                }
                break;
            case "java.lang.String" :
            default:
                if(field.get(zkConfig).toString().length() == 0){
                    throw new NullPointerException(field.getName());
                }
        }
    }

}
